package com.zyb.eduorder.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zyb.commonutils.R;
import com.zyb.eduorder.entity.Order;
import com.zyb.eduorder.entity.PayLog;
import com.zyb.eduorder.entity.vo.OrderQuery;
import com.zyb.eduorder.entity.vo.PayQuery;
import com.zyb.eduorder.service.PayLogService;
import com.zyb.servicebase.exception.GuliException;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 支付日志表 前端控制器
 * </p>
 *
 * @author testJava
 * @since 2022-04-13
 */
@RestController
@RequestMapping("/eduorder/pay-log")
//@CrossOrigin
public class PayLogController {

    @Resource
    private PayLogService payLogService;

    //生成微信支付二维码接口
    //参数是订单号
    @GetMapping("createNative/{orderNo}")
    public R createNative(@PathVariable String orderNo) {
        Map map = payLogService.createNative(orderNo);
        return R.ok().data(map);
    }

    //查询订单支付状态
    //参数：订单号，根据订单号查询 支付状态
    @GetMapping("queryPayStatus/{orderNo}")
    public R queryPayStatus(@PathVariable String orderNo) {
        //查询订单状态
        Map<String, String> map = payLogService.queryPayStatus(orderNo);
        if (map == null) {
            return R.error().message("支付出错了");
        }
        //如果返回map里面不为空，通过map获取订单状态
        if (map.get("trade_state").equals("SUCCESS")) {
            //支付成功
            //添加记录到支付表，更新订单表订单状态
            payLogService.updateOrdersStatus(map);
            return R.ok().message("支付成功");
        }
        return R.ok().code(25000).message("支付中");
    }

    //条件查询带分页支付列表
    @PostMapping("getPayPageCondition/{page}/{limit}")
    public R getOrderPageCondition(@PathVariable Long page, @PathVariable Long limit,
                                   @RequestBody PayQuery payQuery) {
        IPage<PayLog> payOrderPage = payLogService.getPayOrderPage(page, limit, payQuery);
        List<PayLog> records = payOrderPage.getRecords();
        long total = payOrderPage.getTotal();
        return R.ok().data("rows", records).data("total", total);
    }

    //删除一个支付记录
    @DeleteMapping("deletePayLog/{id}")
    public R deletePayLog(@PathVariable String id) {
        boolean b = payLogService.removeById(id);
        if (b) {
            return R.ok();
        }
        return R.error();
    }
}

